<template>
  <div>

    <el-row>
      <el-col :span="24" id="CoachType1">

        <div id="CoachType2">
          <el-input v-model="param.coachTypeName" placeholder="请输入类别名" style="width: 20%;"></el-input>
          <el-button type="primary" @click="search" round>搜索</el-button>
          <el-button type="primary" @click="addCoachType" round>新增类别</el-button>
        </div>

        <div style="height: 20px;"></div>

        <el-table :data="page.records" border>
          <el-table-column prop="coachTypeId" label="教练类型ID" width="115">
          </el-table-column>
          <el-table-column prop="coachTypeName" label="类型名" width="130">
          </el-table-column>
          <el-table-column prop="coachTypeState" label="类型状态" width="130">
            <template slot-scope="scope">
              <template v-if="scope.row.coachTypeState == 1">
                正常
              </template>
              <template v-else>
                禁用
              </template>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="150">
            <template #default="scope">
              <el-button @click="disableType(scope.row.coachTypeId)" v-if="scope.row.coachTypeState == '1'" type="text"
                size="small">禁用</el-button>
              <el-button @click="enableType(scope.row.coachTypeId)" v-if="scope.row.coachTypeState == '0'" type="text"
                size="small">启用</el-button>
              <el-button @click="deleteType(scope.row.coachTypeId)" v-if="scope.row.coachTypeState == '0'" type="text"
                size="small">删除</el-button>
            </template>
          </el-table-column>

        </el-table>

        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :current-page="page.pageNum" :page-sizes="[1, 2, 3, 5]" :page-size="page.pageSize"
          layout="total, sizes, prev, pager, next, jumper" :total="page.total">
        </el-pagination>


        <el-dialog title="新增类别" :visible.sync="showAddCoachType" width="30%">
          <el-input v-model="add.coachTypeName" placeholder="请输入新类别名"></el-input>
          <el-button @click="showAddCoachType = false">取 消</el-button>
          <el-button @click="addCoach()" type="primary">确 定</el-button>

        </el-dialog>

      </el-col>
    </el-row>
  </div>
</template>

<script>
  export default {
    name: "CoachTypeManagement",
    data() {
      return {
        add: {}, // 新增的对象
        showAddCoachType: false, //新增窗口
        page: {},
        param: {
          coachTypeName: "",
          pageNum: 1,
          pageSize: 5,
        },
      }
    },
    methods: {
      //删除
      deleteType(classTypeId) {
        this.$http.get("/account/yCoachType/deleteById", {
          params: {
            classTypeId: classTypeId
          }
        }).then(resp => {
          this.$message({
            message: resp.data.msg,
            type: 'success'
          });
          this.loadAllCoachType();
        });
      },
      //新增方法
      addCoach() {
        this.$http.post("account/yCoachType/addCoachType", this.add).then(resp => {
          this.$message({
            message: resp.data.msg,
            type: 'success'
          });
          this.showAddCoachType = false;
          this.loadAllCoachType();
        });
      },
      //新增窗口
      addCoachType() {
        this.showAddCoachType = true;
      },
      //搜索按钮
      search() {
        this.param.pageNum = 1;
        this.loadAllCoachType();
      },
      //启用
      enableType(coachTypeId) {
        this.$http.get("/account/yCoachType/enable", {
          params: {
            coachTypeId: coachTypeId
          }
        }).then(resp => {
          this.$message({
            message: resp.data.msg,
            type: 'success'
          });
          this.loadAllCoachType();
        });
      },
      //警用
      disableType(coachTypeId) {
        this.$http.get("/account/yCoachType/disable", {
          params: {
            coachTypeId: coachTypeId
          }
        }).then(resp => {
          this.$message({
            message: resp.data.msg,
            type: 'success'
          });
          this.loadAllCoachType();
        });
      },
      handleSizeChange(val) {
        this.param.pageSize = val;
        this.loadAllCoachType();
      },
      handleCurrentChange(val) {
        this.param.pageNum = val;
        this.loadAllCoachType();
      },
      loadAllCoachType() {
        this.$http.get("/account/yCoachType/pageAllCoachType", {
          params: this.param
        }).then(resp => {
          this.page = resp.data.data;
        })
      }
    },
    created() {
      this.loadAllCoachType();
    }
  }
</script>

<style>
  #CoachType2 {
    margin-left: -420px;
    line-height: 100%;
    width: 100%
  }

  #CoachType1 {
    line-height: 100%;
    width: 100%
  }
</style>
